package zzps.exeed.kernel.aop.asm;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.util.HashMap;
import java.util.Map;

public class PrimitiveTypeHelper {


    private static final Map<Type,PrimitiveTypeHelper> helperMap = new HashMap();
    public Type type;
    public int opcode;
    public String owner;
    public String packDescriptor;
    public String unpackMethodName;
    public String unpackDescriptor;
    public int returnCode;

    private PrimitiveTypeHelper(Type type, int opcode, String owner, String packDescriptor, String unpackMethodName, String unpackDescriptor, int returnCode) {
        this.type = type;
        this.opcode = opcode;
        this.owner = owner;
        this.packDescriptor = packDescriptor;
        this.unpackMethodName = unpackMethodName;
        this.unpackDescriptor = unpackDescriptor;
        this.returnCode = returnCode;
    }

    static {
        helperMap.put(Type.INT_TYPE, new PrimitiveTypeHelper(Type.INT_TYPE,Opcodes.ILOAD,"java/lang/Integer",
                "(I)Ljava/lang/Integer;","intValue","()I",Opcodes.IRETURN));
        helperMap.put(Type.BYTE_TYPE,
                new PrimitiveTypeHelper(Type.BYTE_TYPE,Opcodes.ILOAD,Type.getInternalName(Byte.class),
                        "(B)Ljava/lang/Byte;","byteValue","()B",Opcodes.IRETURN));
        helperMap.put(Type.SHORT_TYPE,
                new PrimitiveTypeHelper(Type.SHORT_TYPE,Opcodes.ILOAD,Type.getInternalName(Short.class),
                        "(S)Ljava/lang/Short;","shortValue","()S",Opcodes.IRETURN));
        helperMap.put(Type.LONG_TYPE,
                new PrimitiveTypeHelper(Type.LONG_TYPE,Opcodes.LLOAD,Type.getInternalName(Long.class),
                        "(J)Ljava/lang/Long;","longValue","()J",Opcodes.LRETURN));
        helperMap.put(Type.BOOLEAN_TYPE,
                new PrimitiveTypeHelper(Type.BOOLEAN_TYPE,Opcodes.ILOAD,Type.getInternalName(Boolean.class),
                        "(Z)Ljava/lang/Boolean;","booleanValue","()Z",Opcodes.IRETURN));
        helperMap.put(Type.CHAR_TYPE,
                new PrimitiveTypeHelper(Type.CHAR_TYPE,Opcodes.ILOAD,Type.getInternalName(Character.class),
                        "(C)Ljava/lang/Character;","charValue","()C",Opcodes.IRETURN));
        helperMap.put(Type.FLOAT_TYPE,
                new PrimitiveTypeHelper(Type.FLOAT_TYPE,Opcodes.FLOAD,Type.getInternalName(Float.class),
                        "(F)Ljava/lang/Float;","floatValue","()F",Opcodes.FRETURN));
        helperMap.put(Type.DOUBLE_TYPE,
                new PrimitiveTypeHelper(Type.DOUBLE_TYPE,Opcodes.DLOAD,Type.getInternalName(Double.class),
                        "(D)Ljava/lang/Double;","doubleValue","()D",Opcodes.DRETURN));
    }

    public static PrimitiveTypeHelper getHelper(Type type){
        PrimitiveTypeHelper helper = helperMap.get(type);
        return helper;
    }
}
